//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "WriterExtensibleOption.h"
using namespace Cephei::QL::Experimental::Exoticoptions;
#include <gen/QL/Exercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/Instruments/PlainVanillaPayoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/OneAssetOption.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::CWriterExtensibleOption (Cephei::QL::Instruments::IPlainVanillaPayoff^ payoff1, Cephei::QL::IExercise^ exercise1, Cephei::QL::Instruments::IPlainVanillaPayoff^ payoff2, Cephei::QL::IExercise^ exercise2, Cephei::QL::IPricingEngine^ QL_Pricer) : COneAssetOption(CWriterExtensibleOption::typeid)
{
    CPlainVanillaPayoff^ _Cpayoff1;
    CExercise^ _Cexercise1;
    CPlainVanillaPayoff^ _Cpayoff2;
    CExercise^ _Cexercise2;
    try
    {
#ifdef HANDLE
        _phWriterExtensibleOption = NULL;
#endif
        _Cpayoff1 = safe_cast<CPlainVanillaPayoff^> (payoff1);
        _Cpayoff1->Lock();
        boost::shared_ptr<QuantLib::PlainVanillaPayoff>& _payoff1 = static_cast<boost::shared_ptr<QuantLib::PlainVanillaPayoff>&> (_Cpayoff1->GetShared ()); 
        _Cexercise1 = safe_cast<CExercise^> (exercise1);
        _Cexercise1->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise1 = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise1->GetShared ()); 
        _Cpayoff2 = safe_cast<CPlainVanillaPayoff^> (payoff2);
        _Cpayoff2->Lock();
        boost::shared_ptr<QuantLib::PlainVanillaPayoff>& _payoff2 = static_cast<boost::shared_ptr<QuantLib::PlainVanillaPayoff>&> (_Cpayoff2->GetShared ()); 
        _Cexercise2 = safe_cast<CExercise^> (exercise2);
        _Cexercise2->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise2 = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise2->GetShared ()); 
        _ppWriterExtensibleOption = new boost::shared_ptr<QuantLib::WriterExtensibleOption> (new QuantLib::WriterExtensibleOption ( _payoff1,  _exercise1,  _payoff2,  _exercise2 ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppWriterExtensibleOption)->setPricingEngine (_QL_Pricer);
        SetOneAssetOption (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppWriterExtensibleOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff1 != nullptr) _Cpayoff1->Unlock();
        if (_Cexercise1 != nullptr) _Cexercise1->Unlock();
        if (_Cpayoff2 != nullptr) _Cpayoff2->Unlock();
        if (_Cexercise2 != nullptr) _Cexercise2->Unlock();
    }
}
Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::CWriterExtensibleOption (boost::shared_ptr<QuantLib::WriterExtensibleOption>& childNative, Object^ owner) : COneAssetOption(CWriterExtensibleOption::typeid)
{
#ifdef HANDLE
	_phWriterExtensibleOption = NULL;
#endif
	_ppWriterExtensibleOption = &childNative;
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppWriterExtensibleOption));
}
Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::CWriterExtensibleOption (QuantLib::WriterExtensibleOption& childNative, Object^ owner) : COneAssetOption(CWriterExtensibleOption::typeid)
{
#ifdef HANDLE
	_phWriterExtensibleOption = NULL;
#endif
	_ppWriterExtensibleOption = new boost::shared_ptr<QuantLib::WriterExtensibleOption> (&childNative);
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppWriterExtensibleOption));
    _WriterExtensibleOptionOwner = owner;
    _OneAssetOptionOwner = owner;
}

Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::CWriterExtensibleOption (CWriterExtensibleOption^ copy) : COneAssetOption(CWriterExtensibleOption::typeid)
{
#ifdef HANDLE
	_phWriterExtensibleOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppWriterExtensibleOption = new boost::shared_ptr<QuantLib::WriterExtensibleOption> (copy->GetShared());
        _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppWriterExtensibleOption));
    }
}
Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::CWriterExtensibleOption (PLATFORM::Type^ t) : COneAssetOption(CWriterExtensibleOption::typeid)
{
#ifdef HANDLE
	_phWriterExtensibleOption = NULL;
#endif
	if (!t->IsSubclassOf(CWriterExtensibleOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::CWriterExtensibleOption (QuantLib::Handle<QuantLib::WriterExtensibleOption>& childNative, Object^ owner)  : COneAssetOption(CWriterExtensibleOption::typeid)
{
	_phWriterExtensibleOption = &childNative;
	_ppWriterExtensibleOption = &static_cast<boost::shared_ptr<QuantLib::WriterExtensibleOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppWriterExtensibleOption));
    _WriterExtensibleOptionOwner = owner;
}
Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::CWriterExtensibleOption (QuantLib::Handle<QuantLib::WriterExtensibleOption> childNative)  : COneAssetOption(CWriterExtensibleOption::typeid)
{
	_phWriterExtensibleOption = &childNative;
	_ppWriterExtensibleOption = &static_cast<boost::shared_ptr<QuantLib::WriterExtensibleOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppWriterExtensibleOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::CWriterExtensibleOption (QuantLib::WriterExtensibleOption childNative)  : COneAssetOption(CWriterExtensibleOption::typeid)
{
#ifdef HANDLE
	_phWriterExtensibleOption = NULL;
#endif
	_ppWriterExtensibleOption = new boost::shared_ptr<QuantLib::WriterExtensibleOption> (new QuantLib::WriterExtensibleOption (childNative));
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppWriterExtensibleOption));
}
#endif

Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::~CWriterExtensibleOption ()
{
    if (_ppWriterExtensibleOption != NULL)
    {
	    delete _ppWriterExtensibleOption;
        _ppWriterExtensibleOption = NULL;
    }
}
Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::!CWriterExtensibleOption ()
{
    if (_ppWriterExtensibleOption != NULL)
    {
	    delete _ppWriterExtensibleOption;
    }
}
QuantLib::WriterExtensibleOption& Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::GetReference ()
{
    if (_ppWriterExtensibleOption == NULL) throw REFNEW NativeNullException ();
	return **_ppWriterExtensibleOption;
}
boost::shared_ptr<QuantLib::WriterExtensibleOption>& Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::GetShared ()
{
    if (_ppWriterExtensibleOption == NULL) throw REFNEW NativeNullException ();
	return *_ppWriterExtensibleOption;
}
QuantLib::WriterExtensibleOption* Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::GetPointer ()
{
    if (_ppWriterExtensibleOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppWriterExtensibleOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::WriterExtensibleOption>& Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::GetHandle ()
{
	if (_phWriterExtensibleOption == NULL)
	{
		_phWriterExtensibleOption = new Handle<QuantLib::WriterExtensibleOption> (*_ppWriterExtensibleOption);
	}
	return *_phWriterExtensibleOption;
}
#endif
bool Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::HasNative () 
{
	return (_ppWriterExtensibleOption != NULL);
}

Cephei::QL::IExercise^ Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::Exercise2::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	boost::shared_ptr<QuantLib::Exercise> _rv = (boost::shared_ptr<QuantLib::Exercise>)(*_ppWriterExtensibleOption)->exercise2 ( );   
        Cephei::QL::CExercise^ _nrv = REFNEW Cephei::QL::CExercise (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppWriterExtensibleOption)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IPayoff^ Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption::Payoff2::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	boost::shared_ptr<QuantLib::Payoff> _rv = (boost::shared_ptr<QuantLib::Payoff>)(*_ppWriterExtensibleOption)->payoff2 ( );   
        Cephei::QL::CPayoff^ _nrv = REFNEW Cephei::QL::CPayoff (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Exoticoptions::IWriterExtensibleOption^ Cephei::QL::Experimental::Exoticoptions::CWriterExtensibleOption_Factory::Create (Cephei::QL::Instruments::IPlainVanillaPayoff^ payoff1, Cephei::QL::IExercise^ exercise1, Cephei::QL::Instruments::IPlainVanillaPayoff^ payoff2, Cephei::QL::IExercise^ exercise2, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CWriterExtensibleOption ( payoff1,  exercise1,  payoff2,  exercise2,  QL_Pricer);
}
